<template>
  <!-- 表单 -->
  <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <!-- <el-option label="abc" value="1" /> -->
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width:50%"
        placeholder="请选择部门"
        @focus="hFocus"
      />
      <!-- el-tree:来展示已有的部门 -->
      <div v-show="showTree" style="width:50%">
        <el-tree
          :props="{label:'name'}"
          :data="departments"
          @node-click="selectDepartment"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { findChildren } from '@/utils/index.js'

import { getDepartments } from '@/api/departments'
// 导入常量-聘用形式
import types from '@/constant/employees.js'
const hireType = types.hireType
// 导入的数据不能直接在视图中使用，必须在vue组件中定义(data,computed)
console.log('表单...hireType', hireType)
import { addEmployee } from '@/api/employees.js'

export default {
  name: 'EmpDialog',
  data() {
    return {
      showTree: false, // 不显示部门信息
      // hireType: hireType,
      formData: {
        username: '测试', // 用户名
        mobile: '13412345678', // 手机号
        formOfEmployment: '1', // 聘用形式
        workNumber: '3456', // 工号
        departmentName: '行政部', // 部门
        timeOfEntry: '2012-09-12', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      departments: []
    }
  },
  computed: {
    hireType() {
      return hireType
    }
  },
  methods: {
    // 用户在tree上，点击了某个节点
    // department, node 是自动传入的
    selectDepartment(department, node) {
      console.log(department, node)
      if (department.children.length === 0) {
        // 没有子节点，可以选中
        this.formData.departmentName = department.name

        // 隐藏tree
        this.showTree = false
      }
    },
    // 部门输入框，获取焦点
    hFocus() {
      // 发请求
      this.loadDepartments()
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('getDepartments', res)
        // shift会取出数组第一个元素，同时会改变源数组
        res.data.depts.shift() // 删除第一个值

        this.departments = findChildren(res.data.depts, '')// 保存数据

        // 显示tree
        this.showTree = true
      } catch (err) {
        console.log('getDepartmens', err)
      }
    },
    hCancel() {
      this.$emit('close')
    },
    async doAdd() {
      try {
        const res = await addEmployee(this.formData)
        console.log('addEmployee', res)
        this.$message.success('添加成功')
        // 通知父组件做更新
        this.$emit('update-employee')
      } catch (err) {
        console.log('addEmployee', err)
      }
    },
    // doAdd() {

    // },
    hSubmit() {
      // 手动兜底校验
      this.$refs.form.validate(valid => {
        console.log('手动兜底校验', valid)
        if (valid) {
          this.doAdd()
        }
      })
    },
    // 定义重置表单的方法，给父组件调用
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
